﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports DevExpress.Utils

Public Class FormRk
    Public usr As String
    Private en As New entity_cp_ck
    Dim warehousecode As String '仓库编码
    Dim objCA As New clsCPCK
    Dim objProdInfo As New clsProdInfo
    Dim objPgdMes As New clsPgdMes
    Dim objStorageLocationMst As New clsStorageLocationMst
    Dim httpHelper As New HttpHelper
    Private Sub FormRk_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        warehousecode = GetIniFile("WAREHOUSE", "warehouse_code", "", g_szInitFileName)
        setEmptyLOC()
        rkdt.Properties.VistaDisplayMode = DefaultBoolean.True
        rkdt.Properties.VistaEditTime = DefaultBoolean.True
        rkdt.Properties.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss"
        rkdt.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime
        rkdt.Properties.EditFormat.FormatString = "yyyy-MM-dd HH:mm:ss"
        rkdt.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime
        rkdt.Properties.Mask.EditMask = "yyyy-MM-dd HH:mm:ss"
        rkdt.Text = Now
        txtProdId.Focus()
    End Sub

    Private Sub setEmptyLOC()
        Dim dt As DataTable = objStorageLocationMst.getList("yzzck", "0")
        loc.Items.Clear()
        If dt.Rows.Count > 0 Then
            For i As Integer = 0 To dt.Rows.Count - 1
                loc.Items.Add(dt.Rows(i)("location_no"))
            Next
            loc.SelectedIndex = 0
        End If
    End Sub
    Private Sub txtProdId_KeyDown(sender As Object, e As KeyEventArgs) Handles txtProdId.KeyDown
        If e.KeyCode = Keys.Enter Then
            Search()
        End If
    End Sub

    Private Sub Search()
        If txtProdId.Text.Trim = "" Then Exit Sub
        Dim objprodinfo As New clsProdInfo

        Dim dtt As DataTable = objprodinfo.selectProdInfo(txtProdId.Text.Trim) '还需增加查询条件
        If dtt.Rows.Count > 0 Then
            Dim dr As DataRow = dtt.Rows(0)
            txt_prod_spec.Text = dr("prod_spec_id")
            txt_tray_no.Text = dr("tray_no")
            'MsgBox("status " & dr("status").ToString())
            If dr("status").Equals("2") Then

            ElseIf dr("status").Equals("3") Then
                MsgBox("该生产编号的支座已入库！")
                clear()
            Else
                MsgBox("该生产编号当前不是预组装及设定保护胶状态，请核查")
                clear()
            End If
        Else
            MsgBox("未检索到此生产编号")
            clear()
        End If
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        Search()
    End Sub

    Private Sub btnRk_Click(sender As Object, e As EventArgs) Handles btnRk.Click

        'mesRk("10", "ZAGZ06", "02.02.1507", "nc接口测试")
        If String.IsNullOrEmpty(txtProdId.Text.Trim()) Then Exit Sub
        'If String.IsNullOrEmpty(loc.Text.Trim()) Then Exit Sub
        If String.IsNullOrEmpty(rkdt.Text.Trim()) Then Exit Sub

        Dim dtxt As DataTable = objProdInfo.getProdInfoById(txtProdId.Text.Trim(), "2")

        If dtxt.Rows.Count > 0 Then
            Dim dtt As DataTable = objCA.getListByProdId(txtProdId.Text.Trim(), "0")
            If dtt.Rows.Count = 0 Then
                setEN()
                If objCA.intoCK(en) = -2 Then
                    MsgBox("入库失败")
                    Exit Sub
                Else
                    Dim dttp As DataTable = objProdInfo.selectProdInfo(txtProdId.Text.Trim)
                    Dim dtssd As DataTable = objPgdMes.getList(dttp.Rows(0)("b_pgd_yzz_details_sys_id").ToString())
                    Dim b_gtPgdMesProjectCode As String = dtssd.Rows(0)("mes_project_code").ToString()
                    Dim b_gtPgdMesTypeValue As String = dtssd.Rows(0)("type_value").ToString()
                    Dim rnt_int As Integer = mesRk("1", warehousecode, b_gtPgdMesTypeValue, b_gtPgdMesProjectCode)
                    clear()
                    If rnt_int = 1 Then
                        MsgBox("入库成功")
                    Else
                        MsgBox("入库成功,MES入库失败")
                    End If
                End If
            Else
                MsgBox("该生产编号已入库")
            End If
        Else
            MsgBox("未查询到设定保护胶完成的此生产编号信息")
        End If
    End Sub

    Public Sub clear()
        txtProdId.Text = ""
        txt_prod_spec.Text = ""
        txt_tray_no.Text = ""
        rkdt.Text = Now
        loc.Text = ""
        setEmptyLOC()
        txtProdId.Focus()
    End Sub
    Public Sub setEN()
        With en
            .factory_id = "TS"
            .prod_id = txtProdId.Text.Trim()
            .tray_no = txt_tray_no.Text.Trim()
            .wh_no = loc.Text.Trim()
            .into_time = rkdt.Text.Trim()
            .status = "0"
            .into_ope = usr
            .update_time = Now
        End With
    End Sub

    Private Function mesRk(oper_num As String, warehousecode As String, productorname As String, b_gtPgdMesProjectCode As String) As Integer
        Dim rtn_int As Integer = 0
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """rkcount"":""" & Double.Parse(oper_num) & """," '入库数量
        params &= """warehousecode"":""" & warehousecode & """," '入库仓库编码
        params &= """productorname"":""" & productorname & """," '产品编码
        params &= """procedurename"":""" & "预组装" & """," '执行工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """departmentid"":""TS1103105""" '部门id
        params &= "}"
        Dim rtn As String = httpHelper.HttpPostWebService("productFinishedToWarehouse", params)
        Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
        Dim code As String = jo("code").ToString() '输出0
        Dim msg As String = jo("msg").ToString() '输出操作成功
        If code.Equals("0") And msg.Equals("操作成功") Then
            rtn_int = 1
        End If
        Return rtn_int
    End Function

    Private Sub btnRkMes_Click(sender As Object, e As EventArgs) Handles btnRkMes.Click
        Dim dttp As DataTable = objProdInfo.selectProdInfo(txtProdId.Text.Trim)
        Dim dtssd As DataTable = objPgdMes.getList(dttp.Rows(0)("b_pgd_yzz_details_sys_id").ToString())
        Dim b_gtPgdMesProjectCode As String = dtssd.Rows(0)("mes_project_code").ToString()
        Dim b_gtPgdMesTypeValue As String = dtssd.Rows(0)("type_value").ToString()
        Dim rnt_int As Integer = mesRk("1", warehousecode, b_gtPgdMesTypeValue, b_gtPgdMesProjectCode)
    End Sub
End Class


Public Class entity_cp_ck
    Public sys_id As String
    Public factory_id As String
    Public wh_no As String
    Public prod_id As String
    Public tray_no As String
    Public into_time As DateTime
    Public out_time As DateTime
    Public status As String
    Public into_ope As String
    Public out_ope As String
    Public lhj As String
    Public update_time As DateTime
End Class